package com.zenithmind.version.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 分支统计信息实体类 - 遵循单一职责原则，专门处理分支统计数据
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("version_branch_statistics")
@Schema(description = "分支统计信息实体")
public class BranchStatistics extends BaseEntity {

    /**
     * 分支ID
     */
    @Schema(description = "分支ID")
    private String branchId;

    /**
     * 提交数量
     */
    @Schema(description = "提交数量")
    private Integer commitCount;

    /**
     * 领先提交数
     */
    @Schema(description = "领先提交数")
    private Integer aheadCount;

    /**
     * 落后提交数
     */
    @Schema(description = "落后提交数")
    private Integer behindCount;

    /**
     * 最新提交作者
     */
    @Schema(description = "最新提交作者")
    private String latestCommitAuthor;

    /**
     * 最新提交消息
     */
    @Schema(description = "最新提交消息")
    private String latestCommitMessage;

    /**
     * 创建者名称
     */
    @Schema(description = "创建者名称")
    private String creatorName;

    /**
     * 检查是否领先于主分支
     * 
     * @return 是否领先
     */
    public boolean isAhead() {
        return aheadCount != null && aheadCount > 0;
    }
    
    /**
     * 检查是否落后于主分支
     * 
     * @return 是否落后
     */
    public boolean isBehind() {
        return behindCount != null && behindCount > 0;
    }
    
    /**
     * 检查是否与主分支同步
     * 
     * @return 是否同步
     */
    public boolean isUpToDate() {
        return !isAhead() && !isBehind();
    }
    
    /**
     * 检查是否有分歧
     * 
     * @return 是否有分歧
     */
    public boolean hasDivergence() {
        return isAhead() && isBehind();
    }
    
    /**
     * 获取总的差异提交数
     * 
     * @return 差异提交数
     */
    public int getTotalDifference() {
        int ahead = aheadCount != null ? aheadCount : 0;
        int behind = behindCount != null ? behindCount : 0;
        return ahead + behind;
    }
    
    /**
     * 检查是否为活跃分支（最近有提交）
     * 
     * @return 是否活跃
     */
    public boolean isActiveBranch() {
        return commitCount != null && commitCount > 0;
    }
    
    /**
     * 获取分支状态描述
     * 
     * @return 状态描述
     */
    public String getStatusDescription() {
        if (isUpToDate()) {
            return "Up to date";
        } else if (hasDivergence()) {
            return String.format("%d ahead, %d behind", aheadCount, behindCount);
        } else if (isAhead()) {
            return String.format("%d ahead", aheadCount);
        } else if (isBehind()) {
            return String.format("%d behind", behindCount);
        }
        return "Unknown";
    }
}
